***Note: replicators should set directory to open data 
*cd ""

use ESS.dta, clear

* subset on active population (immigrants and natives tend to have different demographics, and aggregates may too heavily reflect that - see paper)
keep if Age>24 & Age<64

*focus on sample from countries with monotonic first stage
keep if Monotonic==1

*education level for each subgroup	
g EduW=total_eduyrs if Female==1   
g EduM=total_eduyrs if Female==0  
g EduI=total_eduyrs if Ingroup==1  
g EduO=total_eduyrs if Ingroup==0   

 
collapse(mean) EduM  EduW EduI  EduO, by(country year)


     		   grstyle init
	 grstyle set graphsize 13cm 13cm
 graph box EduI EduO, nooutsides over(year, label(labsize(small)))  box(1,color(black))  box(2,color(gs8)) ytitle("Median years of education")  title("{bf:Ethnoracial groups}")  intensity(25) b1title(Year of Survey )  legend( order(1 "Ethnoracial majority" 2 "Ethnoracial minority") rows(1) position(12) size(*.93) symxsize(*0.4) region(lstyle(none))  )  marker(1, msymbol(o)  mfcolor(white) msize(small) mlstyle(linestyle))  marker(2, msymbol(d)  mfcolor(white) msize(small) mlstyle(linestyle))  ylabel(8(2)16,  labsize(medium))  ysize(20) ytitle("Median years of education", size(medlarge)) xsize(16) yscale(range(8(2)16))  boxgap(5) ysc(titlegap(4) outergap(1))   note("")
graph save "Graph/Figure9a", replace 

 
     		   grstyle init
	 grstyle set graphsize 13cm 13cm
 graph box EduM EduW, nooutsides over(year, label(labsize(small)))  box(1,color(black))  box(2,color(gs8)) ytitle("Median years of education")  title("{bf:Gender groups}")  intensity(25) b1title(Year of Survey )  legend( order(1 "Men" 2 "Women") rows(1) position(12) size(*.93)   symxsize(*0.4) region(lstyle(none))  )  marker(1, msymbol(o)  mfcolor(white) msize(small) mlstyle(linestyle))  marker(2, msymbol(d)  mfcolor(white) msize(small) mlstyle(linestyle)) ytitle("") ylabel(8(2)16,  labsize(medium)) yscale(range(8(2)16)) ysize(20) xsize(16)   boxgap(5) ysc(titlegap(4) outergap(1))  note("")
graph save "Graph/Figure9b", replace  
 
      		   grstyle init
	 grstyle set graphsize 13cm 24cm
 graph combine "Graph/Figure9a.gph" "Graph/Figure9b.gph",  cols(2) row(1) imargin(l=0 r=0 b=0) ycommon xcommon 
 graph export "Graph/Figure9.tif", replace
 erase "Graph/Figure9a.gph"
 erase "Graph/Figure9b.gph"
 
 